package com.yourwork.service.imp;


import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yourwork.dao.ConsumptionDao;
import com.yourwork.dao.SalaryDao;
import com.yourwork.model.Consumption;
import com.yourwork.model.Wage;
import com.yourwork.service.SalaryService;
import com.yourwork.view.model.EmployeeManageSum;
@Service(value="salaryService")
public class SalaryServiceImp implements SalaryService {
	@Autowired
	@Qualifier(value="salaryDao")
	private SalaryDao salaryDao;
	@Autowired
	@Qualifier(value="recordDao")
	private ConsumptionDao recordDao;
	@Transactional
	public List<EmployeeManageSum> querySalary(int page ,int size) {
		// TODO Auto-generated method stub
		try {
			
		
		
		Pageable pageable = new  PageRequest(page,size,Sort.Direction.DESC, "id");
		List<EmployeeManageSum> listCS = new ArrayList<EmployeeManageSum>();
		List<String> list = recordDao.queryNumber(pageable);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MM");//获取月份字段
		int dateMonth = Integer.parseInt(sdf.format(date))-1;//将月份字段转化为类型
		for(String l:list){
		EmployeeManageSum cs = salaryDao.querySalary(l,dateMonth);//获取当月的员工工资信息
		listCS.add(cs);//将员工工资信息存入List<EmployeeManageSum>集合
		}
		return listCS;} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	public List<Consumption> querySalaryDetails(String number) {
		// TODO Auto-generated method stub	
		try {
			return recordDao.querySalaryDetails(number);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		
	}
	public List<Consumption> querySalaryDetails(int page,int size ,String number) {
		// TODO Auto-generated method stub	
		try {
		int count;
		count = recordDao.countSalaryDetails(number);
		//判断page的值
		if(page<0){page = 0;}
		if(page<=(count/size)&&count%size!=0){page=page+0;}
			else{if(count%size==0){page=count/size-1;}
			else{page = count/size;}}
		Pageable pageable = new PageRequest(page,size,Sort.Direction.ASC,"id");
		return recordDao.querySalaryDetails(number,pageable);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	@Transactional
	public void insertSalary() {
		// TODO Auto-generated method stub
		DecimalFormat df = new DecimalFormat("#0.00");
		List<String> list = recordDao.queryNumber();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MM");//获取月份字段
		SimpleDateFormat sdf_1 = new SimpleDateFormat("yyyy");//获取年份字段
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");//将String类型转换的类型
		int dateMonth = Integer.parseInt(sdf.format(date))-1;//将月份字段转化为类型
		int onDateMonth;//获取是哪一个月的工资
        Date date_1;
		String dateYears = sdf_1.format(date);//存储年份
		if(dateMonth!=1){
			onDateMonth=dateMonth-1;
		}else{
			onDateMonth=12;
		}
		String nowDate = dateYears + "-" + onDateMonth;//时间
		try {
			date_1 = dateFormat.parse(nowDate);//将Sring类型转换为Date类型
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			date_1 =  null;
		}
		for(String l:list){
		EmployeeManageSum cs = salaryDao.querySalary(l,dateMonth);//获取当月的员工工资信息
		if(cs!=null){
		Wage s = new Wage();
		s.setName(cs.getEmployeeManage().getName());
		s.setDate(date_1);
		s.setCount((int)cs.getCount());
		s.setNumber(cs.getEmployeeManage().getNumber());
		s.setWeightingFactor(cs.getEmployeeType().getWeightingFactor());
		s.setSalary(Double.valueOf(df.format(cs.getSumSalary())));
		s.setBasicSalary(cs.getEmployeeType().getBasicSalary());
		s.setCommissionRate(cs.getEmployeeType().getCommissionRate());
		salaryDao.insertSalary(s);}
		}
		
	}
	public int queryMaxPage(int page, int size) {
		// TODO Auto-generated method stub
		int count;
		count = recordDao.countNumber();
		if(count%size==0){page=count/size-1;}
		else{page = count/size;}
		return page;
	}
	public int queryMaxPage(int page, int size,String number) {
		// TODO Auto-generated method stub
		int count;
		count = recordDao.countSalaryDetails(number);
		if(count%size==0){page=count/size-1;}
		else{page = count/size;}
		return page;
	}
}
